<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<style>
    td input {
        width: 100%;
        line-height: 1.5;
        vertical-align: top;
        border-top: 2px solid #f7f7f7; /*边框颜色*/
        outline: none;
    }
    .fixed-table-footer{
        overflow:hidden !important;
    }
</style>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-purchases-edit">
        <div class="form-group">
            <label class="col-sm-3 control-label">internationalExpressNumber</label>
            <div class="col-sm-8">
                <input disabled id="overseasExpressNo" name="overseasExpressNo" th:value="${overseasExpressNo}"
                       class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label"><span class="must_be_color">*</span>overseasCourier</label>
            <div class="col-sm-8">
                <select class="form-control" disabled id="overseasExpressCompanyID" name="overseasExpressCompanyID"
                        th:with="type=${@dict.getType('erp_overseas_courier')}">
                    <option th:each="dict : ${type}" th:text="${dict['dictLabel']}"
                            th:value="${dict['dictValue']}" th:field="${overseasExpressCompanyID}"></option>
                </select>
            </div>
        </div>
        <table id="bootstrap-table" class="table text-nowrap" data-show-columns="true"
               data-mobile-responsive="true"></table>
        <div class="form-group">
            <div class="form-control-static col-sm-offset-9">
                <button type="submit" class="btn btn-primary">submit</button>
                <button onclick="$.modal.close()" class="btn btn-danger" type="button">close</button>
            </div>
        </div>
    </form>
</div>
<div th:include="include::footer"></div>
<script th:src="@{/ajax/libs/select/select2.js}"></script>
<script type="text/javascript">
    var prefix = ctx + "module/purchases";

    $("#form-purchases-edit").validate({
        submitHandler: function (form) {
            edit();
        }
    });

    function edit () {

        var ids = getBootstrapTableValue('id')
        var purchaseQuantity = getBootstrapTableValue('purchaseQuantity');
        var reg = new RegExp("^[0-9]*$");
        var i=0;

        var overseasReceiveQuantity = new Array();
        $(".overseasReceiveQuantity").each(function () {
            var tem_var = $(this).val();
            if (parseInt(tem_var) <= parseInt(purchaseQuantity[i]) && reg.test(tem_var) && tem_var != ""){
                console.log("yes");
                overseasReceiveQuantity.push(tem_var);
            }else {
                overseasReceiveQuantity.push('0');
            }
            i++;
        });

         $.ajax({
             cache: true,
             type: "POST",
             url: ctx + "module/purchases/edit_received",
             data: {
                 "ids": ids,
                 "overseasReceiveQuantity": overseasReceiveQuantity,
             },
             async: false,
             error: function (request) {
                 $.modal.alertError("system error");
             },
             success: function (data) {
                 $.operate.ajaxSuccess(data);
                 setTimeout(function () {
                     $.modal.close()
                     parent.refresh();
                 }, 50)
             }
         });
    }


    function getBootstrapTableValue (name) {
        var reName = new Array();
        var rows = $('#bootstrap-table').bootstrapTable('getData');
        for (var key in rows) {
            var row = rows[key]
            reName.push(row[name])
        }
        return reName;
    }

    $(function () {
        var options = {
            url: prefix + "/list_edit_received",
            modalName: "海外仓签收",
            search: false,
            showFooter:true,
            queryParams: {
                overseasExpressNo: $('#overseasExpressNo').val()
            },
            showRefresh: false,
            showExport: false,
            showToggle: false,
            showColumns: false,
            columns: [
                {
                    field: 'id',
                    align: 'center',
                    title: 'id',
                    visible: false
                },
                {
                    title: 'serialNumber',
                    align: 'center',
                    width: '40',
                    formatter: function (value, row, index) {
                        var pageSize = $('#bootstrap-table').bootstrapTable('getOptions').pageSize;     //通过table的#id 得到每页多少条
                        var pageNumber = $('#bootstrap-table').bootstrapTable('getOptions').pageNumber; //通过table的#id 得到当前第几页
                        return pageSize * (pageNumber - 1) + index + 1;    // 返回每条的序号： 每页条数 *（当前页 - 1 ）+ 序号
                    }
                },
                {
                    field: 'mainSKU',
                    align: 'center',
                    title: 'SKU'
                },
                {
                    field: 'productName',
                    align: 'center',
                    title: 'productName'
                },
                {
                    field: 'purchaseQuantity',
                    align: 'center',
                    width: '40',
                    title: 'quantityShipped',
                    footerFormatter: function (value) {
                        return calculateLineTotal(value,'purchaseQuantity');
                    }
                },
                {
                    field: 'purchaseQuantity',
                    align: 'center',
                    width: '40',
                    title: 'signForQuantity',
                    formatter: function (value, row, index) {
                        var actions = [];
                        if (row.overseasReceiveQuantity != 0){
                            value = row.overseasReceiveQuantity;
                        }
                        actions.push('<input onBlur="sum_qs()" style="text-align: center" type="text" value="' + value + '" class="overseasReceiveQuantity no_input"/>');
                        return actions.join('');
                    },
                    footerFormatter: function (value) {
                        return calculateLineTotal(value,'overseasReceiveQuantity');
                    }
                }],
            onLoadSuccess: function (item, $element) {
                var $footer =  $('.fixed-table-footer');//页脚
                $footer.addClass("totalRowBackground-color");
                scynScroll();//同步表格与页脚滚动条操作
            },
        };
        $.table.init(options);
    });


    function sum_qs() {
        var sum = 0;
        $(".overseasReceiveQuantity").each(function () {
            sum += parseInt($(this).val());
            $(".fixed-table-footer > table > tbody > tr > td:nth-child(5) > div.th-inner").html(sum)
        });
    }
</script>
</body>
</html>
